package com.zzyl.serve.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.domain.dto.NursingPlanDto;
import com.zzyl.serve.domain.vo.NursingPlanVo;
import com.zzyl.serve.domain.vo.NursingProjectPlanVo;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingProjectPlanService;
import org.apache.ibatis.annotations.Case;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author zzyl
 * @date 2025-02-14
 */
@Transactional
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
//        //  1.根据id查询护理计划
//        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
//        NursingPlanVo nursingPlanVo = new NursingPlanVo();
//        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
//
//        //  2.根据护理计划id查询关联的所有护理项目
//        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
//        nursingPlanVo.setProjectPlans(list);
//        return nursingPlanVo;

        //  1.根据id查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);

        // 2.NursingPlan对象封装给 NursingPlanVo对象（第一张表）
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        //  3.根据护理计划id查询护理项目计划列表数据
        QueryWrapper<NursingProjectPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plan_id", nursingPlan.getId());
        List<NursingProjectPlan> projectPlans = nursingProjectPlanService.list(queryWrapper);

        // 将List<NursingProjectPlan>转换为List<NursingProjectPlanVo>
        List<NursingProjectPlanVo> nursingProjectPlanVos = projectPlans.stream().map(list -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            BeanUtils.copyProperties(list, nursingProjectPlanVo);
            nursingProjectPlanVo.setProjectId(list.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(list.getExecuteCycle().toString());
            nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(list.getExecuteFrequency()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());

        // 4.将护理项目计划列表数据封装给 nursingPlanVo对象（第二张表）
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto dto)
    {
        // 保存护理计划
        // 属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);
//        save(nursingPlan);

        // 批量保存护理项目计划关系
        int count = nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
        return count == 0 ? 0 : 1;
    }

    /**
     * 修改护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto dto)
    {
        try {
    //        nursingPlan.setUpdateTime(DateUtils.getNowDate());
    //        return updateById(nursingPlan) ? 1 : 0;
        //  属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);

        //判断dto中的项目列表为空，如果不为空则先删除护理计划与护理项目的关系，然后在批量添加
        if(CollectionUtils.isNotEmpty(dto.getProjectPlans())){
            // 删除护理计划对应的护理项目列表
            nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
            // 批量保存护理计划对应的护理项目列表
            nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
        }

        // 不管项目列表是否为空，都要修改护理计划
        return nursingPlanMapper.updateNursingPlan(nursingPlan);
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
        }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return nursingPlanMapper.deleteNursingPlanByIds(ids);
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        // 删除护理计划与护理项目的关系
        nursingProjectPlanMapper.deleteByPlanId(id);
        // 删除护理计划
        return removeById(id) ? 1 : 0;


//        LambdaQueryWrapper<NursingProjectPlan> queryWrapper = Wrappers.lambdaQuery();
//        queryWrapper.in(NursingProjectPlan::getPlanId,id);
//        nursingProjectPlanService.remove(queryWrapper);
//        return nursingPlanMapper.deleteNursingPlanById(id);

    }
}
